// generator自动化
/*
function run(fn, ...initValues) {
    let gen = fn(...initValues)
    function next(data) {
        let res = gen.next(data)
        if(res.done) {
            console.log(res)
            return
        }
        console.log(res.value)
        next(res.value)
    }
    next()
}

// run(gen, 1)

function f(a, b) {
    console.log('fn..')
    return a + b
}

function* gen(x) {
    x = yield f(x, 100) + 3
    x = yield f(x, 100) + 3
    x = yield f(x, 100) + 3
    return 200
}
*/

function run(fn, ...initValues) {
    let gen = fn(...initValues)
    function next(data) {
        let res = gen.next(data)
        if(res.done) {
            return
        }
        //将resolve返回的值 返回
        res.value.then(data => next(data))
    }
    next()
}

const delayer = () => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("haha")
        }, 1000)
    })
}

function* start() {
    console.log(1)
    yield delayer()
    console.log(2)
    yield delayer()
    console.log(3)
    yield delayer()
    yield delayer()
    return
}

run(start)